import { Component, OnInit } from '@angular/core';
import { MessageService } from '../../../../core/service';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ApiService, urlService } from 'src/app/core/api';
import { NzModalService } from 'ng-zorro-antd';
import { StorageService } from 'src/app/core/sotrage/storage.service';
import { partition } from 'rxjs/operators';

@Component({
  selector: 'app-address',
  templateUrl: './address.component.html',
  styleUrls: ['./address.scss']
})
export class AddressComponent implements OnInit {
  
  url = 'address';
  
  isVisible = false;
  isConfirmLoading = false;
  validateForm: FormGroup;
  fromData:any= {
    isDefault: false
  };
  
  private _coin = this.storage.get('coin');
  private _config:any;
  private _data:any;
  private _selTag:any;//当前选中
  private _altTitle:string;
  private _page:any={
    pageNo:1,
    pages:1,
    total:10,
    name:'翻页'
  } ;

  
  ngOnInit() {
    this.msg.sendMessage(this.url);
    this.loadList();
    
    this._config=[
      {
        name:'联系人',
        key:'tag',
        class:'table-w-150-huanHang'
      },{
        name:'地址',
        key:'address',
      },{
        name:'操作',
        'key':'cz',
        labels:[{
          name:'编辑',
          type:'click'
        },{
          name:'刪除',
          type:'click'
        }]
      }]
    }
    
    showModal(): void {
      this.validateForm.reset();
      this._altTitle = '添加新地址';
      this.isVisible = true;
      this._selTag={
        name :'新增'
      };
    }
  handleOk( fromData ): void {
    this.isConfirmLoading = true;
    this._selTag.name=='编辑'? this.editAdds(fromData): this.addAdds(fromData);
  };
  handleCancel(): void {
    this.isVisible = false;
  };

  onClick(e){
    this._selTag= e;
    switch (e.name) {
      case '编辑':
      this._editFrom(e.data);
        break;
        case '刪除':
        this._delFrom(e.data);
        break;
        case '翻页':
        this._page = e;
        this.loadList();
        break;
      }
  };
  
  private _editFrom(data){//編輯銀行信息
    this.validateForm.reset();
    this._altTitle = '编辑地址';
    this.isVisible = true;
    this.validateForm = this.fb.group({
      address: [ data.address, [ Validators.required ,  Validators.pattern('[a-zA-Z0-9_]{18,48}$') ] ],
      tag: [ data.tag, [] ],
    });
  }
  private _delFrom(data):void{//編輯銀行信息
    this.modalService.confirm({
      nzTitle     : '您确认需要删除此地址?',
      nzContent   : '<b style="color: red;">地址为：'+ data.address + '</b>',
      nzOkText    : '是',
      nzOkType    : 'danger',
      nzOnOk      :  () => this.delAdds(data),
      nzCancelText: '否',
      nzOnCancel  : () => console.log('Cancel')
    });
  };
  
  async loadList() {
    let data = await this._getAJX('获取转账地址列表',{ pageNo: this._page.pageNo , coinName:this._coin['coinName'] });
    this.api.getData(data,false) ? this._successListOk(data.data) : null;
  };
  async delAdds(tag) {
    this._page.pageNo = 1;
    let data = await this._getAJX('用户删除转账地址',{addressId:tag.id});
    this.api.getData(data,{bool:true,msg:'删除成功！'}) ? this._successOk() : this.isVisible = this.isConfirmLoading = false;
  };
  async addAdds(tag) {
    tag.coinName = this._coin['coinName'];
    let data = await this._getAJX('用户创建转账地址',tag);
    this.api.getData(data,{bool:true,msg:'创建地址成功！'}) ? this._successOk() : this.isVisible = this.isConfirmLoading = false;
  };
  async editAdds(tag) {
    tag.coinName = this._coin['coinName'];
    this._selTag.data.addressId = this._selTag.data.id;
    let data = await this._getAJX('用户编辑转账地址',Object.assign(this._selTag.data, tag));
    this.api.getData(data,{bool:true,msg:'修改地址成功！'}) ? this._successOk() : this.isVisible = this.isConfirmLoading = false;
  };
  
  private _successOk():void{
    this.isVisible = this.isConfirmLoading = false;
    this.loadList();
  };
  
  private _successListOk(data):void{
    this._data = data.list;
    this._page.pageNo = data.pageNo;
    this._page.pages = data.pages;
    this._page.total = this._page.pages * 10;
  };
  
  private  _getAJX(url:string, data:any):any{//接口交換
    let success = this.api.post(urlService('dev', url), data).toPromise();
    return success;
  };
  
  
  constructor(
    private api: ApiService,
    private fb: FormBuilder,
    private modalService: NzModalService,
    private storage: StorageService,
    private msg: MessageService
  ) {
    this.validateForm = this.fb.group({
      address: [ '', [ Validators.required, Validators.pattern('[a-zA-Z0-9_]{18,48}$') ] ],
      tag: [ '', [ ] ],
    });
  };
};
